define(['model/new', 'collection/news', 'text!../../tpl/face/news.html', 'util/tip'], function(newModel, News, template, tip){
    var NewView = Backbone.View.extend({

        el: '#news-list',

        initialize: function(options) {
            this.renderafterCallback = options.renderafterCallback;
        },
        render: function() {
            var self = this;
            var query = new Bmob.Query(newModel);
            query.notEqualTo("isbanner", true);
            query.select("title", 'area');
            query.limit(8);
            query.descending("updatedAt");

            var queryNews = function (area, callback) {
                query.equalTo("area", area); //查询美国
                query.find({
                    success: function(results) {
                        self.template = _.template(template);
                        self.$el.append(self.template(results));
                        callback && callback();
                        if(area === 0){
                            self.renderafterCallback && self.renderafterCallback.apply(this, arguments);
                        }
                        return self;
                    },
                    error: function(error) {
                        return self;
                    }
                });
            };

            tip.showTip('加载中...', true);
            queryNews(1, function () {
                queryNews(2, function () {
                    queryNews(3, function () {
                        queryNews(0);
                        tip.hideTip(5);
                    })
                })
            });
        }
	});
	return NewView;
});
